<?php

use \Orm\BaseModels;

class FundrecordAgencyModel extends BaseModels
{
    /**
     * 默认条件
     */
    private $defalut_where = ['hidden' => 0];

    /**
     * 默认字段
     */
    private $default_field = 'id,order_number,user_id,fund_mode,handle_money,usable_money,create_time,operate_type,remarks,operate_type as type_words,operate_type as type,record_type';



    /**
     *  获取单条记录
     * @throws
     */
    public function getOneById($id, $field = '')
    {
        $field = empty($field) ? $this->default_field : $field;
        $dataOne = $this->where($this->defalut_where)->field($field)->find($id);
        return empty($dataOne) ? $dataOne : $dataOne->toArray();

    }

    /**
     * 获取某个用户单条记录(限制当前用户只能查询自己的记录)
     */
    public function getOneRecord($id, $user_id, $field = ''){
        $field = empty($field) ? $this->default_field : $field;
        $where = $this->defalut_where;
        $where['id']        = $id;
        $where['user_id']    = $user_id;
        $dataOne = $this->where($where)->field($field)->find();
        return empty($dataOne) ? $dataOne : $dataOne->toArray();
    }

    /************ 获取器 ************/
    //operateType字段值转换为大分类值
    public function getTypeWordsAttr($value)
    {
        $type = [
            'RECHARGE' => '在线充值',
            'RECHARGE_BACK' => '充值退回',
            'WITHDRAW' => '提现成功',
            'WITHDRAW_BACK' => '提现退回',
            'TRANSFER_INC' => '划款转账',
            'TRANSFER_DEC' => '划款转账',
            'TRANSFERTOPLATFORM' => '转账到平台',
            'MONEYTRANS_INC' => '资金调整',
            'MONEYTRANS_DEC' => '资金调整',
            'WALLET_TRANSFER' => '迁移资金',
            'WALLET_TRANSFER_FREE' => '迁移资金',
            'TRANSFER_OUT' => '资金转出',
            'TRANSFER_IN' => '资金转入',
            'FULLDEPOSIT'=>'代理成功',
            'AGENCY'=>'支付成功',
            'AGENCY_BACK'=>'代理失败',
            'AGENCY_INCOME'=>'收取订金',
            'AGENCY_INCOME_FEE'=>'收取代理费（有订金模式）',
            'AGENCY_INCOME_LATEFEE'=>'收到违约金',
            'AGENCY_RELATION'=>'关系建立成功'
        ];
        //NULL不是错误，是被允许的，视为该operateType值没有所属的大分类
        return array_key_exists($value, $type) ? $type[$value] : '其他';
    }

    //operateType字段值转换为大分类代码值
    public function getTypeAttr($value)
    {
        $type = [
            'RECHARGE' => '1',
            'WITHDRAW' => '2',
            'WITHDRAW_BACK' => '2',
            'LOANAUDIT' => '5',
            'LOANMANAGE' => '5',
            'REPAYMENT_FEE' => '6',
            'REPAYMENT_CAPITAL' => '6',
            'REPAYMENT_BU' => '6',
            'REPAYMENT_FUWU' => '6',
            'INVEST' => '4',
            'INVEST_BACK' => '4',
            'INVEST_REPAYMENT_CAPITAL' => '3',
            'INVEST_REPAYMENT_PREMIUM' => '3',
            'INVEST_REPAYMENT_FEE' => '3',
            'INVEST_REPAYMENT_BU' => '3'
        ];

        //-1不是错误，是被允许的，视为该operateType值没有所属的大分类
        return array_key_exists($value, $type) ? $type[$value] : '-1';
    }
}
